﻿using System;
using ARD.Common;
using ARD.EventRouting;
using SharpObjects.SearchEngine.Common.ApplicationEvents;

namespace SharpObjects.SearchEngine.Common
{
    public class Logging
    {
        public EventBus EventBus { get; set; }

        public Logging(EventBus eventBus)
        {
            this.EventBus = eventBus;
        }

        public void Publish(LogCategory category, Location origin, string message, params object[] parameters)
        {
            LoggingApplicationEvent.LogMessage logMessage = new LoggingApplicationEvent.LogMessage(origin);
            logMessage.LogCategory = category;
            logMessage.Message = string.Format(message, parameters);
            this.EventBus.Publish(typeof(LoggingApplicationEvent), new LoggingApplicationEvent(logMessage));
        }

        public void Publish(LogCategory category, Location origin, Exception ex)
        {
            LoggingApplicationEvent.LogMessage logMessage = new LoggingApplicationEvent.LogMessage(origin);
            logMessage.LogCategory = category;
            logMessage.Message = "EXCEPTION:\n" + ex.Message + "\nSTACK TRACE:\n" + ex.StackTrace;
            this.EventBus.Publish(typeof(LoggingApplicationEvent), new LoggingApplicationEvent(logMessage));
        }


        public void PublishDebug(Location origin, string message, params object[] parameters)
        {
            Publish(LogCategory.Debug, origin, message, parameters);
        }

        public void PublishInfo(Location origin, string message, params object[] parameters)
        {
            Publish(LogCategory.Info, origin, message, parameters);
        }

        public void PublishWarning(Location origin, string message, params object[] parameters)
        {
            Publish(LogCategory.Warning, origin, message, parameters);
        }

        public void PublishError(Location origin, string message, params object[] parameters)
        {
            Publish(LogCategory.Error, origin, message, parameters);
        }

        public void PublishError(Location origin, Exception ex)
        {
            Publish(LogCategory.Error, origin, ex);
        }
    }
}
